package cn.com.xxlibrary.dao.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.com.xxlibrary.dao.IManagerDao;
import cn.com.xxlibrary.orm.ManagerInfoBean;
import cn.com.xxlibrary.util.UtilTools;

@Transactional
@Repository("manDao")
public class ManagerDaoImpl implements IManagerDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	@Override
	public ManagerInfoBean login(ManagerInfoBean manager) {
		Session session = sessionFactory.getCurrentSession();
		// 拼接查询字符串，查询传入的管理员信息
		StringBuilder hql = new StringBuilder()
				.append("from ManagerInfoBean where manLoginName=")
				.append(UtilTools.toSqlString(manager.getManLoginName()))
				.append(" and manPassword=")
				.append(UtilTools.toSqlString(manager.getManPassword()));
		@SuppressWarnings("unchecked")
		// 查询数据库，并将结果转为list
		List<ManagerInfoBean> list = session.createQuery(hql.toString()).list();
		// 如果查到记录，则判断是否启用
		if (list.size() > 0) {
			manager = list.get(0);
			if(manager.getManState() == 0){
				return null;
			}
			manager.setManLastLoginTime(UtilTools.receiveNowTime());
			session.update(manager);
			return manager;
		} else {
			return null;
		}
	}

	@Override
	public void addManager(ManagerInfoBean manager) {
		Session session = sessionFactory.getCurrentSession();
		if (!UtilTools.isNotNullorEmpty(manager.getManCreateTime())) {
			manager.setManCreateTime(UtilTools.receiveNowTime());
		}
		session.saveOrUpdate(manager);
	}

	@Override
	public void changeManager(int id, int state) {
		Session session = sessionFactory.getCurrentSession();
		ManagerInfoBean infoBean = (ManagerInfoBean) session.get(
				ManagerInfoBean.class, id);
		infoBean.setManState(state);
		session.update(infoBean);
	}

	@Override
	public void delManager(int id) {
		Session session = sessionFactory.getCurrentSession();
		session.delete((ManagerInfoBean) session.get(ManagerInfoBean.class, id));
	}

	@Override
	public Map<String, Object> listManager(int start, int pageSize) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM ManagerInfoBean";

		Query query = session.createQuery(hql);

		int c = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(pageSize);

		List<?> list = query.list();

		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("count", c);
		map.put("queryResult", list);

		return map;
	}

	@Override
	public ManagerInfoBean queManager(int id) {
		Session session = sessionFactory.getCurrentSession();
		ManagerInfoBean manager = (ManagerInfoBean) session.get(
				ManagerInfoBean.class, id);
		return manager;
	}

	@Override
	public void changePwd(ManagerInfoBean manager) {
		Session session = sessionFactory.getCurrentSession();
		session.update(manager);
	}

	public List<?> listManagerType() {
		Session session = sessionFactory.getCurrentSession();
		String HQL = "FROM ManagerTypeBean ORDER BY authSort";
		List<?> list = session.createQuery(HQL).list();
		return list;
	}

	
	public void updataManager(ManagerInfoBean manager){
		Session session = sessionFactory.getCurrentSession();
		session.update(manager);
	}
	
}
